/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.source;

import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.ZipFileSet;
import org.apache.tools.ant.types.resources.FileResource;

import ws.quokka.core.metadata.Metadata;
import ws.quokka.core.metadata.MetadataAware;
import ws.quokka.core.plugin_spi.support.Setter;
import ws.quokka.core.plugin_spi.support.TypedProperties;
import ws.quokka.core.repo_spi.RepoArtifact;
import ws.quokka.core.repo_spi.RepoArtifactId;

import ws.quokka.plugin.lifecycle.LifeCycleImpl;

import java.io.File;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 *
 */
public class SourcePlugin extends LifeCycleImpl implements MetadataAware {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    private Metadata metadata;

    //~ Methods --------------------------------------------------------------------------------------------------------

    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }

    public void init() {
        List exported = metadata.getExportedArtifacts();
        assertTrue(exported.size() >= 1, "There are no artifacts defined in this project");

        RepoArtifactId id = ((RepoArtifact)exported.get(0)).getId();

        List artifactNames = TypedProperties.commaSepList(properties().getString("artifactNames", null), true, false);

        if (artifactNames.size() == 0) {
            assertTrue(exported.size() == 1, prefix("artifactNames") + " must be set if there are multiple artifacts");

            if (!id.getType().equals("source")) {
                artifactNames.add(id.getName());
            }
        }

        for (Iterator i = artifactNames.iterator(); i.hasNext();) {
            String name = (String)i.next();
            metadata.addArtifact(new RepoArtifactId(id.getGroup(), name, "source", id.getVersion()), "Source code",
                Collections.EMPTY_MAP);
        }
    }

    public void packageTemplate() {
        String artifactName = properties().getString("artifactName", null);
        List ids = metadata.getArtifactIds("source");
        assertTrue((artifactName != null) || (ids.size() == 1),
            "Multiple artifacts of type 'source' have been defined. Define a separate target for each one and set the artifactName property");

        RepoArtifactId id = (artifactName == null) ? (RepoArtifactId)ids.get(0)
                                                   : metadata.getArtifactId(artifactName, "source");

        utils().mkdir(getLifeCycle().getPackageOutput());

        Jar jar = (Jar)getProject().createTask("jar");
        jar.setDestFile(getPackageFile(id));
        jar.add(properties().getResourceCollection("source"));

        Setter setter = new Setter(properties());
        setter.set(jar,
            new String[] {
                "compress", "keepCompression", "encoding", "filesOnly", "defaultExcludes", "manifest", "filesetManifest",
                "update", "whenManifestOnly", "duplicate", "index", "manifestEncoding", "roundUp", "level"
            });

        jar.perform();
    }

    private File getPackageFile(RepoArtifactId id) {
        return new File(getLifeCycle().getPackageOutput(),
            id.toPathString() + "." + metadata.getType(id.getType()).getExtension());
    }
}
